﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;


public partial class Manager_OrderDetail_OrderDetailFilmOK : System.Web.UI.Page
{
    private int pQuantity = 0;
    private decimal pCapacity = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script>alert('Bạn cần đăng nhập lại để có thể tiếp tục');window.close();</script>");

            return;
        }
        /* else
         {
             clsWebShop cls = new clsWebShop();
             bool fLog = cls.IsShowPrintAutNoneProc(Session["AdminUserID"].ToString());
             if (fLog == false)
             {
                 Response.Write("<script>alert('Bạn không có quyền vào trang này');window.close();</script>");
                 return;
             }
         }
         */
        Ajax.Utility.RegisterTypeForAjax(typeof(Manager_OrderDetail_OrderDetailOK));

        if (!IsPostBack)
        {
            BindData();
        }
        string day = System.DateTime.Now.Day < 10 ? "0" + System.DateTime.Now.Day.ToString() : System.DateTime.Now.Day.ToString();
        string month = System.DateTime.Now.Month < 10 ? "0" + System.DateTime.Now.Month.ToString() : System.DateTime.Now.Month.ToString();
        lblDate.Text = "Ngày " + day + " tháng " + month + " năm " + System.DateTime.Now.Year;
    }



    private void BindData()
    {
        clsWebShop cls = new clsWebShop();
        clsCommon clsCom = new clsCommon();
        DataTable dt;
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("StartDate", "");
        msg.CreateFirstNode("EndDate", "");
        msg.CreateFirstNode("Status", "");
        msg.CreateFirstNode("End_MK", "N");
        msg.CreateFirstNode("OrderID", Request.QueryString["sOrderID"]);
        dt = cls.Get_OrderFilmByCondition(msg.GetXmlStr.ToString()).Tables[0];

        string OrderID = "";
        if (dt.Rows.Count > 0)
        {
            //lblMaDH.Text = dt.Rows[0]["OrderID"].ToString().Substring(4, dt.Rows[0]["OrderID"].ToString().Length - 4);
            OrderID = dt.Rows[0]["OrderID"].ToString();
            lblMaDH.Text = dt.Rows[0]["OrderID"].ToString();
            lblTenKH.Text = dt.Rows[0]["CusNm"].ToString();
            // lblTaxNo.Text = clsCom.FormatDate(dt.Rows[0]["BirthDay"].ToString());
            lblDiaChi.Text = dt.Rows[0]["Address"].ToString();
            lblNoichon.Text = dt.Rows[0]["PlaceNm"].ToString();
            lblSDT.Text = dt.Rows[0]["Phone"].ToString();
            lblEmail.Text = dt.Rows[0]["Email"].ToString();
            lblTaxNo.Text = dt.Rows[0]["TaxNo"].ToString();
            if (lblTaxNo.Text.Trim() == "")
            {
                trTaxNo.Visible = false;
            }




            if (dt.Rows[0]["Note"].ToString().Trim() != "")
            {
                lblChuY.Visible = true;
                lblNote.Text = dt.Rows[0]["Note"].ToString();
            }






            string sSheetName = OrderID;
            string  sFilePath= "..\\..\\Database\\ListFilm\\" + sSheetName + ".csv";
            sFilePath = Server.MapPath(sFilePath);
            string sSheetRange = ConfigurationManager.AppSettings["SheetRange"].ToString();
            CExcel oExc = new CExcel(sFilePath, sSheetName, sSheetRange);
            DataTable dtExcel = new DataTable();
            try
            {
                DataTable dtSrc = new DataTable();
                dtExcel = oExc.GetCSVRows(true);
                if (dtExcel.Rows.Count > 0)
                {
                    DataTable dtFilm = new DataTable();
                    dtFilm.Columns.Add("FilmNo");
                    dtFilm.Columns.Add("FilmEng");
                    dtFilm.Columns.Add("Capacity");

                    for (int i = 0; i < dtExcel.Rows.Count; i++)
                    {
                        if (dtExcel.Rows[i].RowState != DataRowState.Deleted && dtExcel.Rows[i][0].ToString() != "")
                        {
                            DataRow dr = dtFilm.NewRow();
                            dr[0] = dtExcel.Rows[i][0].ToString();//FilmNo
                            dr[1] = dtExcel.Rows[i][1].ToString();
                            dr[2] = dtExcel.Rows[i][2].ToString();
                            dtFilm.Rows.Add(dr);

                        }
                    }
                    grvMaster.DataSource = dtFilm;
                    grvMaster.DataBind();

                }
            }
            catch (Exception ex) { ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('" + ex.Message.ToString() + "');</script>"); }
                    


            System.Web.UI.WebControls.Image myImage = new System.Web.UI.WebControls.Image();
            /*  myImage.Width = Unit.Pixel(100);
              myImage.Height = Unit.Pixel(100);*/
            myImage.ImageUrl = "../Signature/UserSignImages.aspx";
            pnlSign.Controls.Add(myImage);
            lblSignNm.Text = Session["CusName"].ToString();

            lblQuantity.Text = pQuantity.ToString();
            lblTotal.Text = pCapacity.ToString();
        }
    }

    protected void grvMaster_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            pQuantity++;
            pCapacity += ConvertToDecimal(e.Row.Cells[3].Text);
        }
    }

    private int ConvertToInt(string Quantity)
    {
        int dQuantity = 0;
        try
        {
            dQuantity = int.Parse(Quantity);
        }
        catch
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Số lượng không hợp lệ')</script>");

        }
        return dQuantity;
    }

    private decimal ConvertToDecimal(string Quantity)
    {
        decimal dQuantity = 0;
        try
        {
            dQuantity = decimal.Parse(Quantity);
        }
        catch
        {
            return 0;
        }
        return dQuantity;
    }




    private string FormatMoney(decimal dNum)
    {
        string sReturn = "0";
        try
        {
            sReturn = string.Format("{0:###,###,##0}", dNum);
        }
        catch (Exception) { }
        return sReturn;
    }

    private string FormatMoney(string sNum)
    {
        string sReturn = "0";
        try
        {
            sReturn = string.Format("{0:###,###,##0}", decimal.Parse(sNum));
        }
        catch (Exception) { }
        return sReturn;
    }
}
